package com.example.twotwo_project;

import java.text.SimpleDateFormat;
import java.util.Date;

import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;


public class Board_Input extends ActionBarActivity {
	
	Board_DB db;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_input);
		
		TextView tv = (TextView)findViewById(R.id.textView1);
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy년 MM월 dd일 (E 요일)");
		tv.setText(sdf.format(date));
		
		// 뒤로가기 버튼을 위한 액션바
		ActionBar actionBar = getSupportActionBar();
	    actionBar.setDisplayHomeAsUpEnabled(true);
	}
	
	public void insert(View v){
		String msg = null;
		boolean b = false;
		EditText subject = (EditText)findViewById(R.id.editText1);
		EditText content = (EditText)findViewById(R.id.editText2);
		
		Data d = new Data();
		d.subject = subject.getText().toString();
		d.content = content.getText().toString();
		
		db = new Board_DB(getApplicationContext(), "note", null, 1);
		db.getWritableDatabase();
		
		b = db.insert(d);
		if(b){
			msg = "입력이 완료되었습니다.";
			Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
			Intent intent = new Intent(getApplicationContext(), Board_Breast.class);
			startActivity(intent);
		} else {
			msg = "입력중 오류 발생";
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		
		switch(item.getItemId()){
    	// 뒤로가기 버튼을 위해서
		case android.R.id.home:
			finish();
			return true;
		}
        
    	return super.onOptionsItemSelected(item);
	}
}
